from .exts import db

# 身份、菜单多对多连表
permission = db.Table(
    'permission',
    db.Column('role_id', db.Integer, db.ForeignKey('role.id'), primary_key=True),
    db.Column('menu_id', db.Integer, db.ForeignKey('menu.id'), primary_key=True)
)

# 身份、用户多对多连表
user_role = db.Table(
    'user_role',
    db.Column('role_id', db.Integer, db.ForeignKey('role.id'), primary_key=True),
    db.Column('user_id', db.Integer, db.ForeignKey('user.id'), primary_key=True)
)


class UserModel(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='主键ID')
    username = db.Column(db.String(100), unique=True, comment='用户账号')
    name = db.Column(db.String(100), comment='用户昵称')
    sort = db.Column(db.Integer, comment='排序（数字越小排名越靠前）')
    password = db.Column(db.String(255), comment='密码')
    salt = db.Column(db.String(100), comment='密码盐')
    avatar = db.Column(db.String(255), comment='头像')
    introduction = db.Column(db.String(255), comment='个性签名')
    roles = db.relationship('RoleModel', secondary='user_role', backref='users', lazy=True)


class RoleModel(db.Model):
    __tablename__ = 'role'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='主键ID')
    name = db.Column(db.String(100), comment='角色名称')
    code = db.Column(db.String(100), comment='角色代码')
    sort = db.Column(db.Integer, comment='排序（数字越小排名越靠前）')
    status = db.Column(db.Integer, comment='状态，1：正常；0：禁用')
    menus = db.relationship('MenuModel', secondary='permission', backref='roles', lazy=True)


class MenuModel(db.Model):
    __tablename__ = 'menu'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='菜单主键ID')
    parent_id = db.Column(db.Integer, comment='父菜单 id')
    name = db.Column(db.String(100), comment='菜单名称')
    icon = db.Column(db.String(100), comment='icon 图标')
    type = db.Column(db.Integer, comment='类型：1-目录，2-菜单，3-按钮权限，4-外链')
    route_name = db.Column(db.String(100), comment='路由名称')
    path = db.Column(db.String(100), comment='路由地址')
    component = db.Column(db.String(100), comment='组件路径(vue页面完整路径，省略.vue后缀)')
    perm = db.Column(db.String(100), comment='权限标识')
    sort = db.Column(db.Integer, comment='排序（数字越小排名越靠前）')
    redirect = db.Column(db.String(100), comment='跳转路径')
    hidden = db.Column(db.Integer, comment='是否隐藏该路由：1-隐藏，0-显示')
    always_show = db.Column(
        db.Integer,
        comment='如果设置为 1，将始终显示根菜单;如果没有设置 alwaysShow，当 item 有多于一个子路由时，它将变成嵌套模式，否则不显示根菜单'
    )
    no_cache = db.Column(db.Integer, comment='如果设置为 1，页面将不会被缓存(默认为 0)')
